<?php
namespace App\Http\Controllers\Member;

use App\Models\Area;
use Illuminate\Http\Request;
use App\User;
use App\Models\UserOther;
use App\Models\UserExperience;
use App\Models\UserEducational;
use App\Models\UserProjects;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Log;
use App\Models\ResumeStatus;
/**
 *
 * @author Joker
 *        
 */
class ResumeController extends MemberController
{
    
    
    public function index()
    {
        $parentArea = Area::where('pid','=',0)->get();
        $experience = UserExperience::where('user_id','=',$this->uid)->get();
        $educational = UserEducational::where('user_id','=',$this->uid)->get();
        $projects = UserProjects::where('user_id','=',$this->uid)->get();
        $otherInfo = UserOther::where('user_id','=',$this->uid)->first();
        $resumeStatus = ResumeStatus::all();
        if(!(empty($otherInfo)))
        {
            $otherArea = Area::where('area_id','=',$otherInfo->expect_city)->first();
            $otherInfo->cityname = empty($otherArea)?'':$otherArea->title;
        }
        return view('member.resume.resume')->with('resumeStatus',$resumeStatus)
                ->with('experience',$experience)
                ->with('educational',$educational)
                ->with('projects',$projects)
                ->with('otherinfo',$otherInfo)
                ->with('degree',$this->degree)
                ->with('gznx',$this->gznx)
                ->with('salary',$this->salary)
                ->with('gzxz',$this->gzxz)
                ->with('area',$parentArea);    
    }
    
    /**
     * 数据保存
     */
    public function saveUsername(Request $req)
    {
        $data = $req->only('real_name','signature','shenfen','sex','birth','degree','gznx','province','city','phone','email');
        $saveuser = $this->storeUsers($data);
        $saveuser->degree = $this->degree[$saveuser->degree];
        $saveuser->gznx = $this->gznx[$saveuser->gznx];
        $saveuser->gznx .= $saveuser->gznx>0?'工作经验':'';
        $saveuser->age = user_age($saveuser->birth_year, $saveuser->birth_month);
        return $this->formatJson('success','修改成功',$saveuser);
    }
    
    /**
     * 工作经验
     */
    public function experiencen(Request $req)
    {
        $data = $req->only('name','job','start_at','end_at','content','exp_id');
        if($data['exp_id'])
        {
            $exp = UserExperience::where(array('user_id'=>$this->uid,'id'=>$data['exp_id']))->first();
            if(empty($exp))
            {
                return $this->formatJson('error','未知错误！');
            }
        }
        else 
        {
            $exp = new  UserExperience();
        }
        if($data['name'])
            $exp->company_name = $data['name'];
        if($data['job'])
            $exp->job = $data['job'];
        if($data['start_at'])
            $exp->start_at = $data['start_at'];
        if($data['end_at'])
            $exp->end_at = $data['end_at'];
        if($data['content'])
            $exp->content = $data['content'];
        
        $exp->user_id = $this->uid;
        if($exp->save())
            return $this->formatJson('success','成功！',$exp);
        else
            return $this->formatJson('error','失败！');
    }
    
    /**
     * 用户简历－教育经历保存
     * @param Request $req
     */
    public  function educational(Request $req)
    {
        $data = $req->only('name','major','degree','graduated_at','edu_id');
        if($data['edu_id'])
        {
            $edu = UserEducational::where(array('user_id'=>$this->uid,'id'=>$data['edu_id']))->first();
            if(empty($edu))
            {
                return $this->formatJson('error','未知错误！');
            }
        }
        else
        {
            $edu = new  UserEducational();
        }
        if($data['name'])
            $edu->school = $data['name'];
        if($data['major'])
            $edu->major = $data['major'];
        if($data['degree'])
            $edu->degree = $data['degree'];
        if($data['graduated_at'])
            $edu->graduated_at = $data['graduated_at'];
        $edu->user_id = $this->uid;
        if($edu->save()){
            $edu->degrees = $this->degree;
            return $this->formatJson('success','成功！',$edu);
        }else{
            return $this->formatJson('error','失败！');
        }
    }
    
    /**
     * 个人简历－项目经验
     * @param Request $req
     */
    public function projects(Request $req)
    {
        $data = $req->only('name','duty','start_at','end_at','content','url','prj_id');
        if($data['prj_id'])
        {
            $prj = UserProjects::where(array('user_id'=>$this->uid,'id'=>$data['prj_id']))->first();
            if(empty($prj))
            {
                return $this->formatJson('error','未知错误！');
            }
        }
        else
        {
            $prj = new  UserProjects();
        }
        if($data['name'])
            $prj->name = $data['name'];
        if($data['duty'])
            $prj->duty = $data['duty'];
        if($data['start_at'])
            $prj->start_at = $data['start_at'];
        if($data['end_at'])
            $prj->end_at = $data['end_at'];
        if($data['content'])
            $prj->intro = $data['content'];
        if($data['url'])
            $prj->url = $data['url'];
        $prj->user_id = $this->uid;
        
        if($prj->save()){
            return $this->formatJson('success','成功！',$prj);
        }else{
            return $this->formatJson('error','失败！');
        }
    }
    
    /**
     * 用户简历－附加信息
     */
    public function other(Request $req)
    {
        $data = $req->only('content','specialty','expect_job','job_type','expect_province','expect_city','expect_wages','more_infos','resume_status');
        $oth = UserOther::where(array('user_id'=>$this->uid))->first();
        if(empty($oth))
        {
            $oth = new  UserOther();
            $oth->user_id = $this->uid;
        }
        if($data['content']) $oth->intro = $data['content'];
        if($data['specialty']) $oth->specialty = $data['specialty'];
        if($data['expect_job']) $oth->expect_job = $data['expect_job'];
        if($data['job_type']) $oth->job_type = $data['job_type'];
        if($data['expect_province']) $oth->expect_province = $data['expect_province'];
        if($data['expect_city']) $oth->expect_city = $data['expect_city'];
        if($data['expect_wages']) $oth->expect_wages = $data['expect_wages'];
        if($data['more_infos']) $oth->more_infos = $data['more_infos'];
        if(!empty(trim($data['resume_status'])))
        {
            $resumeStatus = ResumeStatus::where('name','=',trim($data['resume_status']))->first();
            if(empty($resumeStatus))
                return $this->formatJson('error','数据异常');
            $oth->resume_status_id = $resumeStatus->id;
        }
            
        
        if($oth->save()){
            $otherAre = Area::where('area_id','=',$oth->expect_city)->first(); 
            $oth->cityname = empty($otherAre) ?'':$otherAre->title;
            $oth->salarys =$this->salary;
            $oth->gzxz = $this->gzxz;
            return $this->formatJson('success','成功！',$oth);
        }else{
            return $this->formatJson('error','失败！');
        }
    }
    
    private function storeUsers($data)
    {
        $saveuser = User::find($this->uid);
        if(empty($saveuser))
            return $this->formatJson('error','用户不存在，出错了！');
        if($data['real_name'])
            $saveuser->real_name = $data['real_name'];
        if($data['signature'])
            $saveuser->signature = $data['signature'];
        $saveuser->save();
        if($data['phone'])
            $saveuser->phone = $data['phone'];
        if($data['sex'])
            $saveuser->sex = $data['sex'];
        if($data['birth']){
            $birth = explode("-", $data['birth']);
            $saveuser->birth_year = $birth[0];
            $saveuser->birth_month = $birth[1];
        }
        if($data['degree'])
            $saveuser->degree = $data['degree'];
        if($data['province'])
            $saveuser->province = $data['province'];
        if($data['city'])
            $saveuser->city = $data['city'];
        if($data['gznx'])
            $saveuser->gznx = $data['gznx'];
        if($data['email'])
            $saveuser->email = $data['email'];
        if($data['shenfen'])
            $saveuser->shenfen = $data['shenfen'];
        $saveuser->save();
            
        return $saveuser;
    }
    
    /**
     * 删除简历
     * @param interger $id
     */
    public function destroy($id)
    {
        $app = Input::get('app');
        if($app == 'experience')
        {
            $entity = UserExperience::where(['id'=>$id,'user_id'=>$this->uid])->first();
        }
        elseif($app == 'educational')
        {
            $entity = UserEducational::where(['id'=>$id,'user_id'=>$this->uid])->first();
        }
        elseif($app == 'projects')
        {
            $entity = UserProjects::where(['id'=>$id,'user_id'=>$this->uid])->first();
        }
        else
        {
            return $this->formatJson('error','参数错误！');
        }
        if(empty($entity))
            return $this->formatJson('error','参数错误！');
        
        try {
            $entity->delete();
        }catch (\PDOException $e){
            Log::info(['删除简历信息失败'=>$e->getMessage()]);
            return $this->formatJson('error','未知错误！');
        }
        return $this->formatJson('success','删除成功',$entity);
    }
    
}